const express = require('express')
const AiConfigController = require('../controllers/aiConfigController')
const { validateAiConfigCreate, validateAiConfigUpdate, validatePagination } = require('../middleware/validation')
const { authenticateToken, requireConfigOwnership, optionalAuth } = require('../middleware/auth')
const { aiLimiter } = require('../middleware/security')

const router = express.Router()

// 创建AI配置
router.post('/',
  authenticateToken,
  validateAiConfigCreate,
  AiConfigController.createConfig
)

// 获取AI配置列表
router.get('/',
  optionalAuth,
  validatePagination,
  AiConfigController.getUserConfigs
)

// 获取单个AI配置
router.get('/:configId',
  authenticateToken,
  requireConfigOwnership,
  AiConfigController.getConfig
)

// 更新AI配置
router.put('/:configId',
  optionalAuth,
  validateAiConfigUpdate,
  AiConfigController.updateConfig
)

// 删除AI配置
router.delete('/:configId',
  authenticateToken,
  requireConfigOwnership,
  AiConfigController.deleteConfig
)

// 设置默认AI配置
router.put('/:configId/default',
  optionalAuth,
  AiConfigController.setDefaultConfig
)

// 测试AI配置连接
router.post('/:configId/test',
  authenticateToken,
  requireConfigOwnership,
  aiLimiter,
  AiConfigController.testConfig
)

// 切换AI配置状态
router.put('/:configId/toggle',
  authenticateToken,
  requireConfigOwnership,
  AiConfigController.toggleConfigStatus
)

// 复制AI配置
router.post('/:configId/duplicate',
  authenticateToken,
  requireConfigOwnership,
  AiConfigController.duplicateConfig
)

// 获取默认AI配置（支持未认证用户）
router.get('/default/config',
  optionalAuth,
  AiConfigController.getDefaultConfig
)

// 获取支持的AI模型列表
router.get('/models/supported',
  authenticateToken,
  AiConfigController.getSupportedModels
)

// 获取支持的AI服务提供商列表
router.get('/providers/supported',
  authenticateToken,
  AiConfigController.getSupportedProviders
)

// 验证AI配置参数
router.post('/validate/parameters',
  authenticateToken,
  AiConfigController.validateParameters
)

module.exports = router